

#import "GoodsModel.h"
#import "MenuModel.h"
#import "NetWorkTool.h"

@implementation GoodsModel

- (void)setMenuModelArr:(NSArray<MenuModel *> *)categories
{
    NSMutableArray *nmArr = [NSMutableArray array];
    for (NSDictionary *dict in categories) {
        [nmArr addObject:[MenuModel menuModelWithDict:dict]];
    }
    _categories = nmArr.copy;
}

+(instancetype)goodsModelWithDict:(NSDictionary *)dict
{
    GoodsModel *model = [[GoodsModel alloc] init];
    [model setValuesForKeysWithDictionary:dict];
    return model;
}

+(void)modelWithSuccess:(void (^)(NSArray<GoodsModel *> *,NSDictionary *))successBlock error:(void (^)())errorBlock
{
    [[NetWorkTool shareManager] POST_RequestWithUrlString:@"http://iosapi.itcast.cn/loveBeen/supermarket.json.php" parmas:@{@"call":@5} successBloock:^(NSDictionary *responseObject) {
        
        NSDictionary *dataDict = responseObject[@"data"];
        NSDictionary *products = dataDict[@"products"];
        NSArray *cateArr = dataDict[@"categories"];
        
        NSMutableArray *nmArr = [NSMutableArray array];
        
        for (NSDictionary *dict in cateArr) {
            MenuModel *model = [MenuModel menuModelWithDict:dict];
            [nmArr addObject:model];
        }
        
        if (successBlock) {
            successBlock(nmArr.copy,products);
        }
        
    } failureBlock:^(NSError *error) {
        NSLog(@">>>>>>%@",error);
    }];
}

@end
